package com.ruoyi.flow.flow.identity.activiti;

import com.ruoyi.flow.core.exception.BadRequestException;
import com.ruoyi.flow.flow.identity.activiti.entity.FlowDept;
import com.ruoyi.flow.flow.identity.activiti.entity.FlowUser;
import com.ruoyi.flow.flow.identity.activiti.entity.FlowUserDeptRelation;
import com.ruoyi.flow.flow.identity.activiti.repository.FlowDeptRepository;
import com.ruoyi.flow.flow.identity.activiti.repository.FlowUserDeptRelationRepository;
import com.ruoyi.flow.flow.identity.activiti.repository.FlowUserRepository;
import com.google.common.collect.Lists;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.GroupQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.data.GroupDataManager;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wtian
 * @version 1.0
 * @since 2018/9/6 9:45
 */
public class FlowGroupDataManager implements GroupDataManager {
    private static final Logger LOGGER = LoggerFactory.getLogger(FlowGroupDataManager.class);
    @Resource
    private FlowDeptRepository deptRepository;
    @Resource
    private FlowUserDeptRelationRepository userDeptRelationRepository;
    @Resource
    private FlowUserRepository userRepository;

    @Override
    public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) {
        if (StringUtils.isNotEmpty(query.getUserId())) {
            return findGroupsByUser(query.getUserId());
        } else if (StringUtils.isNotEmpty(query.getId())) {
            List<Group> groups = Lists.newArrayList();
            groups.add(findById(query.getId()));
            return groups;
        } else if (StringUtils.isNotEmpty(query.getName())) {
            List<Group> groups = Lists.newArrayList();
            groups.add(deptRepository.findByName(query.getName()));
            return groups;
        } else if (StringUtils.isNotEmpty(query.getType())) {
            return Lists.newArrayList();
        } else if (StringUtils.isNotEmpty(query.getNameLike())) {
            List<Group> groups = Lists.newArrayList();
            groups.addAll(deptRepository.findByNameLike(query.getNameLike()));
            return groups;
        } else {
            List<Group> groups = Lists.newArrayList();
            groups.addAll(deptRepository.findAll());
            return groups;
        }
    }

    @Override
    public long findGroupCountByQueryCriteria(GroupQueryImpl query) {
        return findGroupByQueryCriteria(query, null).size();
    }

    @Override
    public List<Group> findGroupsByUser(String userId) {
        FlowUser flowUser = userRepository.findById(userId);
        if (Objects.nonNull(flowUser)) {
            List<FlowUserDeptRelation> userDeptRelations = userDeptRelationRepository.findByUseridAndDeptidIsNot(flowUser.getUserid(), -1);
            List<Group> groups = Lists.newArrayList();
            for (FlowUserDeptRelation userDeptRelation : userDeptRelations) {
                FlowDept flowDept = userDeptRelation.getDept();
                if (Objects.nonNull(flowDept)) {
                    flowDept.setType(FlowDept.GROUPTYPE.ASSIGNMENT.getType());
                }
                groups.add(userDeptRelation.getDept());
            }
            return groups;
        } else {
            return Lists.newArrayList();
        }
    }

    @Override
    public List<Group> findGroupsByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
        throw new BadRequestException("Flow user manager does not support this option.");
    }

    @Override
    public long findGroupCountByNativeQuery(Map<String, Object> parameterMap) {
        throw new BadRequestException("Flow user manager does not support this option.");
    }

    @Override
    public GroupEntity create() {
        throw new BadRequestException("Flow user manager does not support this option.");
    }

    @Override
    public GroupEntity findById(String entityId) {
        FlowDept dept = deptRepository.findById(entityId);
        dept.setType(FlowDept.GROUPTYPE.ASSIGNMENT.getType());
        return dept;
    }

    @Override
    public void insert(GroupEntity entity) {
        throw new BadRequestException("Flow user manager does not support this option.");
    }

    @Override
    public GroupEntity update(GroupEntity entity) {
        throw new BadRequestException("Flow user manager does not support this option.");
    }

    @Override
    public void delete(String id) {
        throw new BadRequestException("Flow user manager does not support this option.");
    }

    @Override
    public void delete(GroupEntity entity) {
        throw new BadRequestException("Flow user manager does not support this option.");
    }
}
